package ua.org.nuos.sdms.clientgui.client;

import com.vaadin.Application;
import com.vaadin.terminal.DownloadStream;
import com.vaadin.terminal.URIHandler;
import com.vaadin.terminal.gwt.server.HttpServletRequestListener;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.ui.Window;
import ua.org.nuos.sdms.clientgui.client.context.NotificationManager;
import ua.org.nuos.sdms.clientgui.client.context.UserContext;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.CompleteUserRegistrationEvent;
import ua.org.nuos.sdms.clientgui.client.events.ErrorMessageEvent;
import ua.org.nuos.sdms.clientgui.client.events.InfoMessageEvent;
import ua.org.nuos.sdms.clientgui.client.events.ShowDialogMessageEvent;
import ua.org.nuos.sdms.clientgui.client.handlers.CompleteUserRegistrationHandler;
import ua.org.nuos.sdms.clientgui.client.listeners.CompleteUserRegistrationListener;
import ua.org.nuos.sdms.clientgui.client.listeners.ShowDialogMessageListener;
import ua.org.nuos.sdms.clientgui.client.listeners.ShowErrorMessageListener;
import ua.org.nuos.sdms.clientgui.client.listeners.ShowInfoMessageListener;
import ua.org.nuos.sdms.clientgui.client.view.RootUiHandler;
import ua.org.nuos.sdms.clientgui.server.AppProxy;

import javax.annotation.PreDestroy;
import javax.ejb.Remove;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URL;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 02.10.11
 * Time: 19:31
 * To change this template use File | Settings | File Templates.
 */
@SessionScoped
public class ClientApplication extends Application implements HttpServletRequestListener {

    private static final String CODE_PARAMETER = "code";

    @Inject
    private UserContext userContext;

    @Inject
    private AppProxy appProxy;

    public UserContext getUserContext() {
        return userContext;
    }

    @Override
    public void init() {
        Window main = new Window("SDMS");
        setMainWindow(main);
        setTheme("sdms");
        main.getContent().setSizeFull();
        getMainWindow().addURIHandler(URIHandler(main));
        userContext.getAppData().setMainWindow(main);
    }

    private void reloadBoard(final Window main) {
        AppProxy.getInstance().getControllerLocator().registerController(new GlobalBoard());
        AppProxy.getInstance().getController(GlobalBoard.ID).register(ShowErrorMessageListener.class, ErrorMessageEvent.class);
        AppProxy.getInstance().getController(GlobalBoard.ID).register(ShowInfoMessageListener.class, InfoMessageEvent.class);
        AppProxy.getInstance().getController(GlobalBoard.ID).register(ShowDialogMessageListener.class, ShowDialogMessageEvent.class);
        AppProxy.getInstance().getController(GlobalBoard.ID).register(CompleteUserRegistrationListener.class, CompleteUserRegistrationEvent.class);

        AppProxy.getInstance().getController(GlobalBoard.ID).addListener(new NotificationManager(main));
        AppProxy.getInstance().getController(GlobalBoard.ID).addListener(new CompleteUserRegistrationHandler());
    }

    private URIHandler URIHandler(final Window main) {
        return new URIHandler() {

            @Override
            public DownloadStream handleURI(URL url, String s) {
                resetData();
                reloadBoard(main);
                String code = userContext.getAppData().getRequest().getParameter(CODE_PARAMETER);
                if (code != null && !code.isEmpty()) {
                    AppProxy.getInstance().getController(GlobalBoard.ID).fire(new CompleteUserRegistrationEvent(code));
                }
                main.getContent().removeAllComponents();
                main.setContent(new RootUiHandler());
                return null;
            }
        };
    }

    private void resetData() {
        userContext.getControllerLocator().resetData();
    }

    @Override
    public void onRequestStart(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        userContext.getAppData().setRequest(httpServletRequest);
        userContext.getAppData().setResponse(httpServletResponse);
    }

    @Override
    public void onRequestEnd(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        userContext.getAppData().setRequest(null);
        userContext.getAppData().setResponse(null);
    }

    @PreDestroy
    public void unbound() {
        ((WebApplicationContext) getContext()).valueUnbound(null);
        userContext = null;
    }

    @Remove
    @Override
    public void close() {
        userContext = null;
        super.close();
    }
}
